home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////
- // Pocket PC Game Programming
- // Chapter 15: Infrared and Socket Communications
- //
- // Project01.cpp - Infrared Test
- //
- //
- //////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "Project01.h"
-
- //////////////////////////////////////////////////////////////////////
- // GameInit
- // Triggered by WinMain before window is created
- //////////////////////////////////////////////////////////////////////
- BOOL GameInit(HINSTANCE hInst)
- {
- //create new game library objecti
- Game = new CGameLibrary(hInst, _T("Project01"));
- if (Game == NULL)
- return FALSE;
-
- //set up the game window for creation
- Game->SetFullscreen(TRUE);
- Game->G_SetDisplay(FALSE);
- Game->SetTitle(_T("Infrared Test"));
- Game->SetFrameRate(FRAME_RATE);
-
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- // GameStart
- // Triggered by WndProc after game window is activated
- //////////////////////////////////////////////////////////////////////
- void GameStart(HWND hWnd)
- {
- HDC hdc;
-
- // seed the random number generator
- srand(GetTickCount());
-
- //create double buffer
- cbDoubleBuffer = new CBitmap(GetDC(hWnd));
- if (!cbDoubleBuffer->Create(Game->ScreenWidth(), Game->ScreenHeight()))
- Game->FatalError(_T("Error creating double buffer"));
-
- //use the double buffer
- hdc = cbDoubleBuffer->GetSourceDC();
-
- //load background image
- cbBackground = new CBitmap(hdc);
- if (!cbBackground->Load(Game->GetPath(L"infrared.bmp")))
- Game->FatalError(_T("Error loading infrared.bmp"));
-
- //draw background image to double buffer
- cbBackground->BitBlit(0, 0);
-
- }
-
- //////////////////////////////////////////////////////////////////////
- // GameActivate
- // Triggered by WndProc when game is activated
- //////////////////////////////////////////////////////////////////////
- void GameActivate(HWND hWnd)
- {
- }
-
- //////////////////////////////////////////////////////////////////////
- // GamePaint
- // Window repaint event (usually triggered once at startup)
- //////////////////////////////////////////////////////////////////////
- void GamePaint(HWND hWnd)
- {
- HDC hdc;
- PAINTSTRUCT ps;
-
- hdc = BeginPaint(hWnd, &ps);
-
- //draw double buffer to screen
- BitBlt(hdc, 0, 0, Game->ScreenWidth(), Game->ScreenHeight(),
- cbDoubleBuffer->GetSourceDC(), 0, 0, SRCCOPY);
-
- EndPaint(hWnd, &ps);
- }
-
- //////////////////////////////////////////////////////////////////////
- // GameEvent
- // Triggered by the message loop in WinMain
- //////////////////////////////////////////////////////////////////////
- void GameEvent()
- {
- HWND hWnd;
- HDC hdc;
-
- hWnd = Game->GetWindow();
- hdc = GetDC(hWnd);
-
- //draw background image to double buffer
- cbBackground->BitBlit(0, 0);
-
- CheckInfrared(cbDoubleBuffer->GetSourceDC());
-
- //draw double buffer to screen
- BitBlt(hdc, 0, 0, Game->ScreenWidth(), Game->ScreenHeight(),
- cbDoubleBuffer->GetSourceDC(), 0, 0, SRCCOPY);
-
- //never delete the main DC, only release it!
- ReleaseDC(hWnd, hdc);
-
- }
-
- //////////////////////////////////////////////////////////////////////
- // CheckInfrared
- //
- //////////////////////////////////////////////////////////////////////
- void CheckInfrared(HDC hdc)
- {
- SOCKET irSocket;
- char chBuffer[1024];
- char szHostName[1024];
- HOSTENT *lphostent;
- in_addr address;
- int nSize;
- int sockRet;
- TCHAR szTemp[30];
- DEVICELIST *pDevList;
- int line = 5, xPos = 4;
- WSADATA wsaData;
-
-
- //check the winsock library
- sockRet = WSAStartup(MAKEWORD(1,1), &wsaData);
- if (sockRet != 0)
- {
- Game->PrintText(hdc, _T("Winsock library not found!"), xPos, line, RGB(255,255,255), TRANSPARENT);
- return;
- }
-
- //winsock will return version requested if valid
- if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
- {
- Game->PrintText(hdc, _T("Winsock library version is invalid!"), xPos, line, RGB(255,255,255), TRANSPARENT);
- WSACleanup();
- return;
- }
-
- //display winsock version
- wsprintf(szMessage, _T("Winsock version: %i.%i"),
- HIBYTE(wsaData.wHighVersion), LOBYTE(wsaData.wHighVersion));
- Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
-
- //display max sockets
- wsprintf(szMessage, _T("Maximum sockets: %i"), wsaData.iMaxSockets);
- Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
-
- //display max packet size
- //if limit is 0, use implicit limit of 8192
- sockRet = wsaData.iMaxUdpDg;
- if (sockRet == 0)
- sockRet = 8192;
- wsprintf(szMessage, _T("Maximum packet size: %i"), sockRet);
- Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
-
- //display buffer size
- nSize = sizeof(chBuffer);
- wsprintf(szMessage, _T("Socket buffer size: %i"), nSize);
- Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
-
- //find ip address
- sockRet = gethostname(szHostName, 1024);
- if (sockRet == SOCKET_ERROR)
- {
- Game->PrintText(hdc, _T("Could not find host name"), xPos, line++, RGB(255,255,255), TRANSPARENT);
- WSACleanup();
- return;
- }
-
- //display host name
- mbstowcs(szTemp, szHostName ,1024);
- wsprintf(szMessage, _T("Local host name: %s"), szTemp);
- Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
-
- //display ip address
- lphostent = gethostbyname(szHostName);
- if (lphostent == NULL)
- {
- Game->PrintText(hdc, _T("Error finding ip address"), xPos, line++, RGB(255,255,255), TRANSPARENT);
- WSACleanup();
- return;
- }
-
- for (int i = 0; lphostent->h_addr_list[i] != NULL; i++)
- {
- memcpy(&address, lphostent->h_addr_list[i], sizeof(address));
- wsprintf(szMessage, _T("Local host ip: %i.%i.%i.%i"),
- address.S_un.S_un_b.s_b1,
- address.S_un.S_un_b.s_b2,
- address.S_un.S_un_b.s_b3,
- address.S_un.S_un_b.s_b4);
- Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
- }
-
- //open the socket port
- irSocket = socket(AF_IRDA, SOCK_STREAM, 0);
- if (irSocket == INVALID_SOCKET)
- {
- Game->PrintText(hdc, _T("Error opening infrared port!"), xPos, line++, RGB(255,255,255), TRANSPARENT);
- WSACleanup();
- return;
- }
-
- line++;
- Game->PrintText(hdc, _T("Infrared socket opened"), xPos, line++, RGB(255,255,255), TRANSPARENT);
-
- sockRet = getsockopt(irSocket, SOL_IRLMP, IRLMP_ENUMDEVICES, chBuffer, &nSize);
- if (sockRet == SOCKET_ERROR)
- {
- Game->PrintText(hdc, _T("Error receiving device list"), xPos, line++, RGB(255,255,255), TRANSPARENT);
- closesocket(irSocket);
- WSACleanup();
- return;
- }
-
- Game->PrintText(hdc, _T("Received device list"), xPos, line++, RGB(255,255,255), TRANSPARENT);
-
- pDevList = (DEVICELIST*) chBuffer;
- if (pDevList->numDevice > 0)
- {
- Game->PrintText(hdc, _T("Infrared device found!"), xPos, line++, RGB(255,255,255), TRANSPARENT);
- mbstowcs(szTemp, pDevList->Device[0].irdaDeviceName,
- sizeof(pDevList->Device[0].irdaDeviceName));
- wsprintf(szMessage, _T("Remote device name: %s"), szTemp);
- Game->PrintText(hdc, szMessage, xPos, line++, RGB(255,255,255), TRANSPARENT);
- }
- else
- {
- Game->PrintText(hdc, _T("No infrared activity"), xPos, line++, RGB(255,255,255), TRANSPARENT);
- }
-
- closesocket(irSocket);
-
- WSACleanup();
- }
-
- //////////////////////////////////////////////////////////////////////
- // StylusDown
- // Stylus press event
- //////////////////////////////////////////////////////////////////////
- void StylusDown(int x, int y)
- {
- //tap the screen to end program
- if (x > 45 && x < 195 && y > 275)
- Game->Shutdown();
- }
-
- //////////////////////////////////////////////////////////////////////
- // GameEnd
- // End of program event
- //////////////////////////////////////////////////////////////////////
- void GameEnd()
- {
- HWND hWnd = Game->GetWindow();
- HDC hdc = GetDC(hWnd);
-
- delete Game;
- delete cbDoubleBuffer;
- delete cbBackground;
- DeleteDC(hdc);
-
- }
-
- //////////////////////////////////////////////////////////////////////
- // StylusMove
- // Stylus drag across screen event
- //////////////////////////////////////////////////////////////////////
- void StylusMove(int x, int y)
- {
- }
-
- //////////////////////////////////////////////////////////////////////
- // StylusUp
- // Stylus release event
- //////////////////////////////////////////////////////////////////////
- void StylusUp(int x, int y)
- {
- }
-
- void ButtonPress(int iButtonID, POINT pt)
- {
- }
-
- void ButtonRelease(int iButtonID, POINT pt)
- {
- }
-
-